package com.leon.programPeral.ch3.ext4;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.Date;

public class Ch3Ext4DateFormatClient
{
	public static void main(String[] args)
	{
		//		diff("2016-07-27", "2017-07-30");
		//		diff("2019-04-27", "2100-01-20");
		//		diff("2016-02-29", "2017-07-20");
		weekday("2019-07-27");
		weekday("2019-07-28");
		weekday("2019-07-29");
	}

	private static void weekday(String weekday)
	{
		String base = "2019-07-21";
		int i = dataDiffSelf(base, weekday);
		System.out.println(i % 7);
	}

	private static void diff(String date1, String date2)
	{
		System.out.println(date1 + "   " + date2);
		System.out.println(dateDiff(date1, date2));
		System.out.println(dataDiffSelf(date1, date2));
	}

	/**
	 * 自己设计算法实现日期差，不使用 java 内置的工具类
	 * @param date1 第一个日期 2018-07-27
	 * @param date2 第二个日期 2019-07-27
	 * @return 天数差
	 */
	private static int dataDiffSelf(String date1, String date2)
	{
		int[] months = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		//		System.out.println(JSONObject.toJSONString(months));
		int year1 = Integer.parseInt(date1.substring(0, 4));
		int year2 = Integer.parseInt(date2.substring(0, 4));
		int res = 0;
		// 首先计算天数差
		int d1 = Integer.parseInt(date1.substring(8, 10));
		int d2 = Integer.parseInt(date2.substring(8, 10));

		res += d2 - d1;

		// 再计算月份的天数差
		int m1 = Integer.parseInt(date1.substring(5, 7));
		int m2 = Integer.parseInt(date2.substring(5, 7));

		if (m2 > m1)
		{
			for (int i = m1; i < m2; i++)
			{
				res += months[i];
			}
			if (d2 < d1 && m2 < m1)
			{
				res -= months[m2];
			}
		}
		else if (m2 < m1)
		{
			for (int i = m2; i < m1; i++)
			{
				res -= months[i];
			}
		}

		// 最后计算年份的差异
		res += (year2 - year1) * 365;
		for (int i = year1; i < year2; i++)
		{
			if (isLeapYear(i))
			{
				res++;
			}
		}
		if (isLeapYear(year1) && m1 > 2)
		{
			res--;
		}
		if (isLeapYear(year2) && m2 > 2)
		{
			res++;
		}
		return res;
	}

	/**
	 * 判断是否是闰年
	 * @param year 需要判断的年份
	 * @return true / false
	 */
	private static boolean isLeapYear(int year)
	{
		return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
	}

	@Test
	public void testLeapYear()
	{
		System.out.println(isLeapYear(2018));
		System.out.println(isLeapYear(2016));
		System.out.println(isLeapYear(2000));
		System.out.println(isLeapYear(2100));
		System.out.println(isLeapYear(1999));
		System.out.println(isLeapYear(2019));
	}

	/**
	 * 计算距离1970 年的时间戳，再计算两个时间差
	 * @param date1
	 * @param date2
	 * @return
	 */
	private static int dateDiffv2(String date1, String date2)
	{
		Timestamp d1 = Timestamp.valueOf(date1 + " 00:00:00");
		Timestamp d2 = Timestamp.valueOf(date2 + " 00:00:00");
		return (int) ((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000L));
	}

	/**
	 * 如 date1 < date2
	 * @param date1 2019-07-27
	 * @param date2 2019-07-30
	 * @return
	 */
	private static int dateDiff(String date1, String date2)
	{
		Timestamp d = Timestamp.valueOf(date1 + " 00:00:00");
		Timestamp d2 = Timestamp.valueOf(date2 + " 00:00:00");
		int diff = 0;
		while (d.getTime() < d2.getTime())
		{
			d = new Timestamp(d.getTime() + 24 * 60 * 60 * 1000L);
			diff++;
		}
		return diff;

	}
}
